﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ershou.BLL;
using System.IO;

public partial class jg_xgsp : System.Web.UI.Page
{
    sp sp1 = new sp();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            int id = int.Parse(Request.QueryString["ID"].Trim());
            if (!IsPostBack)
            {
                Image1.ImageUrl = sp1.select17(id);
                TextBox2.Text = sp1.select7(id);
                TextBox3.Text = sp1.select4(id);
                TextBox4.Text = sp1.select5(id);
                TextBox5.Text = sp1.select15(id);
                TextBox6.Text = sp1.select12(id);
                TextBox7.Text = sp1.select9(id);
                DropDownList1.SelectedValue = sp1.select14(id);
                if (Session["img"] != null)
                {
                    Image1.ImageUrl = Session["img"].ToString();
                }
            }
        }
        Label1.Text = ""; 
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (sp1.select13(TextBox2.Text.Trim()))
        {
            Label1.Text = "该商品名已存在，请重新命名！";
        }
        else if(sp1.select16(int.Parse(Request.QueryString["ID"].Trim())) =="拍卖中")
        {
            Label1.Text = "拍卖中的商品无法修改！";
        }
        else
        {
            string n = TextBox2.Text.Trim();
            int m = int.Parse(Session["CustomerId"].ToString().Trim());
            string t = DropDownList1.SelectedValue.Trim();
            int p = int.Parse(TextBox3.Text.Trim());
            string s = TextBox7.Text;
            int z = int.Parse(TextBox4.Text.Trim());

            string x = TextBox6.Text.ToString().Trim();
            DateTime d = DateTime.Parse(x);
            int pro = int.Parse(TextBox5.Text.Trim());

            string i = "~/Image/sp/" + n + Path.GetExtension(FileUpload1.FileName);
            if (FileUpload1.PostedFile.ContentLength != 0)
            {
                string filePath = Server.MapPath("~/") + "Image\\sp\\" + n + Path.GetExtension(FileUpload1.FileName);
                File.Delete(filePath);
                FileUpload1.PostedFile.SaveAs(filePath);
            }
            if (FileUpload1.PostedFile.ContentLength == 0)
            {
                sp1.UpdateOrder4(int.Parse(Request.QueryString["ID"].Trim()), n, m, t, p, s, z, d, pro);
            }
            else
            {
                sp1.UpdateOrder3(int.Parse(Request.QueryString["ID"].Trim()), n, m, t, p, s, i, z, d, pro);
            }
            Response.Redirect("~/jg/grsp.aspx");
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.ContentLength != 0)
        {
            string filePath = Server.MapPath("~/") + "Image\\cs\\" + Session["CustomerId"] + Path.GetExtension(FileUpload1.FileName);
            File.Delete(filePath);
            FileUpload1.PostedFile.SaveAs(filePath);
        }
        if (FileUpload1.PostedFile.ContentLength == 0)
        {
            Label1.Text = "请先导入图片！";
        }
        else
        {
            Image1.ImageUrl = "~/Image/cs/" + Session["CustomerId"] + Path.GetExtension(FileUpload1.FileName);
        }
        Session["img"] = Image1.ImageUrl;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}